package com.smartscreen.schedulemanage.service.impl;

import com.smartscreen.schedulemanage.dto.UpdateTimetableSessionRequest;
import com.smartscreen.schedulemanage.entity.TimetableSession;
import com.smartscreen.schedulemanage.mapper.TimetableMapper;
import com.smartscreen.schedulemanage.service.TimetableService;
import com.smartscreen.baseinfomanage.client.BaseInfoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TimetableServiceImpl implements TimetableService {
    
    @Autowired
    private TimetableMapper timetableMapper;
    
    @Autowired
    private BaseInfoClient baseInfoClient;

    @Override
    public String importClassTimetable(String classId, MultipartFile file) {
        return "";
    }

    @Override
    public Map<String, Object> getClassTimetable(String classId, String termId) {
        List<TimetableSession> sessions = timetableMapper.getClassTimetable(classId, termId);
        List<TimetableSession> enrichedSessions = enrichTimetableSessions(sessions);
        
        Map<String, Object> result = new HashMap<>();
        result.put("sessions", enrichedSessions);
        result.put("total", enrichedSessions.size());
        
        return result;
    }
    
    @Override
    public Map<String, Object> getTeacherTimetable(String teacherId, String termId) {
        List<TimetableSession> sessions = timetableMapper.getTeacherTimetable(teacherId, termId);
        List<TimetableSession> enrichedSessions = enrichTimetableSessions(sessions);
        
        Map<String, Object> result = new HashMap<>();
        result.put("sessions", enrichedSessions);
        result.put("total", enrichedSessions.size());
        
        return result;
    }

    @Override
    public void updateTimetableSession(String sessionId, UpdateTimetableSessionRequest request) {

    }

    private List<TimetableSession> enrichTimetableSessions(List<TimetableSession> sessions) {
        if (sessions.isEmpty()) {
            return sessions;
        }
        
        // 收集所有需要查询的ID
        Set<String> subjectIds = sessions.stream().map(TimetableSession::getSubjectId).collect(Collectors.toSet());
        Set<String> teacherIds = sessions.stream().map(TimetableSession::getTeacherId).collect(Collectors.toSet());
        Set<String> classIds = sessions.stream().map(TimetableSession::getClassId).collect(Collectors.toSet());
        Set<String> venueIds = sessions.stream().map(TimetableSession::getVenueId).collect(Collectors.toSet());
        
        // 批量查询基础信息
        Map<String, Map<String, Object>> subjects = baseInfoClient.getSubjectsBatch(subjectIds.stream().toList());
        Map<String, Map<String, Object>> teachers = baseInfoClient.getTeachersBatch(teacherIds.stream().toList());
        Map<String, Map<String, Object>> classes = baseInfoClient.getClassesBatch(classIds.stream().toList());
        Map<String, Map<String, Object>> venues = baseInfoClient.getVenuesBatch(venueIds.stream().toList());
        
        // 填充名称信息
        for (TimetableSession session : sessions) {
            if (subjects.containsKey(session.getSubjectId())) {
                session.setSubjectName((String) subjects.get(session.getSubjectId()).get("name"));
            }
            if (teachers.containsKey(session.getTeacherId())) {
                session.setTeacherName((String) teachers.get(session.getTeacherId()).get("name"));
            }
            if (classes.containsKey(session.getClassId())) {
                session.setClassName((String) classes.get(session.getClassId()).get("name"));
            }
            if (venues.containsKey(session.getVenueId())) {
                session.setVenueName((String) venues.get(session.getVenueId()).get("name"));
            }
        }

        return sessions;
    }
    
    // ... 其他方法实现
}